//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tools\fn_spawnGroup.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_group","_units","_position","_radius","_side","_skill","_type","_unit","_init","_pos","_instance_id"];
DEFAULT_PARAM(_instance_id,0,TMF_SERVER_INSTANCE_ID);
DEFAULT_PARAM(_units,1,[]);
DEFAULT_PARAM(_side,2,GVAR(SIDE_ENEMY));
DEFAULT_PARAM(_position,3,ARR_3(0,0,0));
DEFAULT_PARAM(_radius,4,0);
DEFAULT_PARAM(_skill,5,([] call FFUNC(getUnitSkill)));
DEFAULT_PARAM(_init,6,[]);

if (_instance_id == "") then { _instance_id = TMF_SERVER_INSTANCE_ID; };

_group = grpNull;
if (!IS_SCALAR(_radius)) then { _radius = 0 };
if (!IS_ARRAY(_position)) then {
	if (AND_ALSO(IS_STRING(_position),IS_MARKER(_position))) then {
		if (_radius <= 0) then {
			_radius = ((markerSize _position) select 0) max ((markerSize _position) select 1);
		};
		_position = markerPos _position;
	} else {
		_position = [];
	};
};
if (!IS_SCALAR(_skill)) then { _skill = ([] call FFUNC(getUnitSkill)) };


if (AND_ALSO(IS_ARRAY(_units),((count _units > 0) && (count _position == 3)))) then {
	_group = [_side] call FFUNC(createGroup);
	if (!isnull _group) then {
		{
			_type = _x;
			if (_type isKindOf "Man") then {
				_pos = _position;
				_pos set [2, 0];
				_unit = [_instance_id,_type,_group,_position,0,_init,"",_skill] call FFUNC(createUnit);
			} else {
				if (_type isKindOf "AllVehicles") then {
					_unit = [_instance_id,_type,_group,_position,0,_init,random(360),_skill] call FFUNC(spawnVehicle);
				};
			};
		} foreach _units;
		if (count (units _group) > 0) then {
			(leader _group) setRank "CORPORAL";
		} else {
			ERROR("Failed to spawn group");
			_group call FFUNC(deleteGroup);
			_group = grpNull;
		};
	} else {
		ERROR("Failed to create group");
	};
} else {
	ERROR("Invalid Parameters");
};

TRACE_1("End",ARR_2(_group,(units _group)));
TMFPROFILERSTOP;
_group;